using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Windows.Forms;
using System.Security.Cryptography;
namespace DAO
{
    public class NhanVien : Interface
    {
        string s = Application.StartupPath;
        public static string GetMD5(string chuoi)
        {
            string str_md5 = "";
            byte[] mang = System.Text.Encoding.UTF8.GetBytes(chuoi);

            MD5CryptoServiceProvider my_md5 = new MD5CryptoServiceProvider();
            mang = my_md5.ComputeHash(mang);

            foreach (byte b in mang)
            {
                str_md5 += b.ToString("X2");
            }

            return str_md5;
        }

        public static bool PhatSinhMaNhanVien(DTO.NhanVien nv)
        {
            string sql = "select MaNhanVien from NhanVien";
            DataTable dt = Interface.SelectTest(sql);
            if (dt != null)
            {
                if(dt.Rows.Count == 0)
                {
                    nv.MaNhanVien = "1001";
                }
                else
                {
                    DataRow dr = dt.Rows[dt.Rows.Count - 1];
                    int ma = int.Parse(dr["MaNhanVien"].ToString());
                    ma++;
                    nv.MaNhanVien = ma.ToString();
                }
                
                return true;
            }
            else
                return false;
        }

        public static bool ThemNhanVien(DTO.NhanVien nv)
        {
            string pw = GetMD5("123");
            string sql = "insert into NhanVien (MaNhanVien, HoTen, DiaChi, NgaySinh, DienThoai, BangCap, BoPhan, ChucVu ,Password) values (N'";
            sql += nv.MaNhanVien + "',N'";
            sql += nv.HoTen + "',N'";
            sql += nv.DiaChi + "','";
            sql += nv.NgaySinh + "','";
            sql += nv.DienThoai + "','";
            sql += nv.MaBangCap + "','";
            sql += nv.MaBoPhan + "','";
            sql += nv.MaChucVu + "',N'";
            sql += pw + "')";
            bool result = Interface.InsertTest(sql);
            return result;
        }

        public string LayMaNhanVien(string tenNhanVien)
        {
            Provider p = new Provider("Server=.\\SQLEXPRESS;AttachDbFilename=" + s + "\\QLTV.mdf; Trusted_Connection=Yes;Integrated security=true;User Instance=true");
            DataTable dt = p.ExecuteQuery("SELECT MaNhanVien, HoTen FROM NhanVien");
            if(dt == null)
            {
                Provider p1 = new Provider("Server=.\\;AttachDbFilename=" + s + "\\QLTV.mdf; Trusted_Connection=Yes;Integrated security=true;User Instance=true");
                dt = p1.ExecuteQuery("SELECT MaNhanVien, HoTen FROM NhanVien");
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (tenNhanVien.Trim() == dt.Rows[i]["HoTen"].ToString().Trim())
                    return dt.Rows[i]["MaNhanVien"].ToString();
            }
            return "";
        }

        public DataTable LayDanhSachNhanVien(string nameSP)
        {
            Provider p = new Provider("Server=.\\SQLEXPRESS;AttachDbFilename=" + s + "\\QLTV.mdf; Trusted_Connection=Yes;Integrated security=true;User Instance=true");
            DataTable dt = p.ExecuteStoreProcedure(nameSP);
            if(dt == null)
            {
                Provider p1 = new Provider("Server=.\\;AttachDbFilename=" + s + "\\QLTV.mdf; Trusted_Connection=Yes;Integrated security=true;User Instance=true");
                dt = p1.ExecuteStoreProcedure(nameSP);
            }
            return dt;
        }
    }
}
